﻿using MaterialDesignThemes.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindow.Commands;
using UIWindow.Domains;
using UIWindow.GlobalParameters;
using UIWindow.Helper;
using UIWindow.ViewModels.Dialogs;
using UIWindow.Views.Dialogs;

namespace UIWindow.ViewModels
{
    public class ParameterConfigurationViewModel
    {
        private log4net.ILog _log = log4net.LogManager.GetLogger("message");
        public ParameterGlobal MyParameter { get; set; }
        private UserGlobal _userGlobal;
        public ParameterConfigurationViewModel(ParameterGlobal parameter, UserGlobal userGlobal)
        {
            MyParameter = parameter;
            _userGlobal = userGlobal;
            EnableMES = true;
        }
        private bool _enableMES;

        public bool EnableMES
        {
            get { return _enableMES; }
            set
            {
                _enableMES = value;
                MyParameter.DecideParameter.EnableMES = value;
                if (!GlobalStart.IsDeBug) _ = DialogHost.Show(new LoginDialogs() { DataContext = new LoginDialogsViewModel() }, "RootDialog").ConfigureAwait(false);
            }
        }

        public DelegateCommand CheckBarCode => new DelegateCommand(async () =>
        {
            if (string.IsNullOrEmpty(MyParameter.RunParameter.ElectrolyteMaterialNumber) || string.IsNullOrEmpty(MyParameter.RunParameter.BatchElectrolyte))
            {
                $"请输入正确的电解液条码".MessageDialogs(Enums.MessageLevelType.Error);
                return;
            }
            _ = DialogHost.Show(new LoadingDialogs(), "RootDialog").ConfigureAwait(false);
            bool blos = await Task.Run(()=> MyMesHelper.CellBOMItemsCheck(null,
                        MyParameter.RunParameter.MoNumber,
                        _userGlobal.LoginUser.Name,
                        MyParameter.RunParameter.EquipNum, $"{MyParameter.RunParameter.ElectrolyteMaterialNumber}:{MyParameter.RunParameter.BatchElectrolyte};{MyParameter.RunParameter.BatchElectrolyteNumber}", 3));
            DialogHost.Close("RootDialog");
            if (blos)
            {
                $"电解液条码验证通过!!!".RunLog(Enums.MessageLevelType.Info);
            }
            else
            {
                $"电解液条码验证不通过".RunLog(Enums.MessageLevelType.Error);
            }
        });
        public DelegateCommand SaveExecute => new DelegateCommand(() =>
        {
            MyParameter.Save();
            _log.Info($"{_userGlobal.LoginUser.Name} 修改参数");
            "参数修改，保存成功！！！".MessageDialogs(Enums.MessageLevelType.Info);
        });
    }
}
